<template>
  <FModal header="集团基准岗" @ok="ok" ref="refFModal">
    <div class="copy-group-position-body">
      <el-table height="100%" border stripe :default-sort="sortConfig"
                @row-click="rowClick" :data="dataSource" @sort-change="sortChange"
                @selection-change="selectionChange" v-loading="loading">
        <el-table-column type="selection" width="40"/>
        <el-table-column show-overflow-tooltip prop="positionName" label="岗位名称"/>
        <el-table-column show-overflow-tooltip prop="positionCode" label="岗位编码"/>
        <el-table-column show-overflow-tooltip prop="systemPreset" label="系统预制">
          <template #default="{row}">
            <SystemDict v-model="row.systemPreset" disabled dict-code="booleanYesNot"/>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="sort" label="排序号"/>
      </el-table>
    </div>
  </FModal>
</template>
<script setup>
import FModal from '@/components/FModal/index.vue';
import {shallowRef} from 'vue';
import TableSearchUtils from '@/utils/TableSearchUtils';
import {getAction, postAction} from '@/request/BaseRequest';
import SystemDict from '@/components/SystemDict/index.vue';
import {isEmpty, real} from '@/utils/CommonUtils';
import {ElMessage} from 'element-plus';

const refFModal = shallowRef();
const searchParams = {};
const emits = defineEmits(['copyedRoles']);
let unitData = undefined;

function setData(_unitData) {
  unitData = real(_unitData);
  refFModal.value.open();
  search();
}

const searchForm = {
  value: {
    validate(callback) {
      callback(true);
    },
  }
};

function doSearch(params) {
  loading.value = true;
  getAction('/sys/position/getGroupPositions').then(data => {
    loading.value = false;
    if (data.success) {
      let result = data.result;
      setSearchResult({
        records: result
      });
    }
  });
}

function onSelection() {

}

function onRowClick() {

}

function getPositionIds() {
  let list = real(selectedRows);
  if (isEmpty(list)) return [];
  let res = list.map(item => item.id);
  return res;
}

function ok() {
  let positionIds = getPositionIds();
  if (isEmpty(positionIds)) {
    ElMessage({
      message: '请选择岗位',
      type: 'warning',
    })
    return;
  }
  loading.value = true
  postAction('/sys/position/copyGroupPosition', {
    positionIds,
    unitId: real(unitData).id
  }).then(data => {
    loading.value = false
    if (data.success) {
      emits('copyedRoles');
      refFModal.value.close();
    }
  })
}

const {
  loading, rowClick, search, selectionChange, setSearchResult, pageOptions, pageChange,
  sortChange, dataSource, resetQueryParams, sortConfig, selectedRows, pagination
} = TableSearchUtils({
  refForm: searchForm,
  doSearch,
  defaultSortConfig: {
    prop: 'sort',
    order: 'ascending',
  },
  searchParams,
  selectionChange: onSelection,
  rowClick: onRowClick
});

defineExpose({
  setData
});
</script>
<script>
export default {
  name: 'CopyGroupPositionModal'
};
</script>
<style>
.f-modal-body .copy-group-position-body {
  height: 500px;
}
.f-modal-fullscreen-body .copy-group-position-body {
  height: 100%;
}
</style>
